********************************************************************************
*                                                                              *
*                             REGRESSION ANALYSES                              *
*                                                                              *
********************************************************************************

cd "H:\"

// Variable list for fixed effects regressions
global feweightvar hoursworked09
global fevar sbi4#fyear mun#fyear 
global fevarcompete sbi2#fyear mun#fyear 
global xvar age1824 age2534 age3544 age4554 age5567 hh_single hh_other
global xvaradd male migrant migrant_2ndgen
global cluster beidXyear rinpersoon
global clustercompete industry_ifrXyear rinpersoon

// What do we do below:
*1) Worker-level competition analysis for full sample on hourly wage
*2) Worker-level competition analysis on hourly wage, by worker type
*3) First-stage results
*4) Worker-level competition analysis for full sample on hours worked and employment
*5) Worker-level competition analysis on hours worked and employment, by worker type
*6) Worker-level competition analysis on personal income, by worker type

////////////////////////////////////////////////////////////////////////////////
///////////////////1) COMPETITION INSTRUMENTED -- MAIN EFFECTS//////////////////
////////////////////////////////////////////////////////////////////////////////	

* Create Table 6

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)

		
ivreghdfe loghwage (compete=robotsexposure_tmin5) [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl20
ivreghdfe loghwage (compete=robotsexposure_tmin5) $xvar $xvaradd [weight=indweight], absorb(beid  $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl21
ivreghdfe loghwage (compete=robotsexposure_tmin5) $xvar [weight=indweight], absorb(beid  $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl22

qui estimates restore repl20
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_baseline", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon  nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl21
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_baseline", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl22
    outreg2 using "Robots and Workers\Results\Worker_competeIV_baseline", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
	
	
	
////////////////////////////////////////////////////////////////////////////////
////////////////////2) COMPETITION INSTRUMENTED BY WORKER TYPE//////////////////
////////////////////////////////////////////////////////////////////////////////	
	
* Create Table 7	
	
use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1
	keep if robotsfirm == 0
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	// Create relevant variables
	capture g logincome_adj = ln(income_adj)
	capture g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture egen beidXyear = group(beid year)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)
	egen industry_ifrXyear = group(industry_ifr year)

	g competeXrepl = compete*replaceable
	g competeXnonrepl = compete*(1-replaceable)
	g competeXmeffect = compete*meffect
	g competeXnonmeffect = compete*(1-meffect)
	g competeXlowskilled = compete*lowskilled
	g competeXnonlowskilled = compete*(1-lowskilled)
	g competeXmedskilled = compete*medskilled
	g competeXnonmedskilled = compete*(1-medskilled)
	g competeXhighskilled = compete*(1-lowmedskilled)

	g robotsexpXreplaceable = robotsexposure_tmin5*replaceable
	g robotsexpXnonreplaceable = robotsexposure_tmin5*(1-replaceable)
	g robotsexpXmeffect = robotsexposure_tmin5*meffect
	g robotsexpXnonmeffect = robotsexposure_tmin5*(1-meffect)
	g robotsexpXlowskilled = robotsexposure_tmin5*lowskilled
	g robotsexpXnonlowskilled = robotsexposure_tmin5*(1-lowskilled)
	g robotsexpXmedskilled = robotsexposure_tmin5*medskilled
	g robotsexpXnonmedskilled = robotsexposure_tmin5*(1-medskilled)
	g robotsexpXhighskilled = robotsexposure_tmin5*(1-lowmedskilled)

ivreghdfe loghwage (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl1
ivreghdfe loghwage (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl2
ivreghdfe loghwage                   (competeXmeffect=robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl3	
ivreghdfe loghwage (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl4
ivreghdfe loghwage (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl5
ivreghdfe loghwage                (competeXrepl=robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl6
ivreghdfe loghwage (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl7
ivreghdfe loghwage (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl8
ivreghdfe loghwage                   (competeXlowskilled=robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl9	
	
	
qui estimates restore repl1
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster replace word tex ctitle(Hourly wage \textit{(log)}) nocon  nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl2
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl3
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl4
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl5
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl6
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl7
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl8
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl9
	outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hwage", label aster append word tex ctitle(Hourly wage \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))	

	
		
////////////////////////////////////////////////////////////////////////////////
///////////////////////3) COMPETITION FIRST-STAGE RESULTS///////////////////////
////////////////////////////////////////////////////////////////////////////////	
	
* Create Table C11

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	keep if hwweight == 1
	sort rinpersoon year
	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	capture egen beidXyear = group(beid year)
	keep if include==1 & include_post2009==1
	g logincome_adj = ln(income_adj)
	g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee ==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)
	egen industry_ifrXyear = group(industry_ifr year)

reghdfe compete robotsexposure_tmin5 [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl41
reghdfe compete robotsexposure_tmin5 $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl42
reghdfe compete robotsexposure_tmin5 $xvar $xvaradd [weight=indweight], absorb(beid rinpersoon $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl43
	
	qui estimates restore repl41
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_FS", label aster replace word tex ctitle(Compete) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl42
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_FS", label aster append word tex ctitle(Compete) nocon
qui estimates restore repl43
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_FS", label aster append word tex ctitle(Compete) nocon	
	
////////////////////////////////////////////////////////////////////////////////
///////////////4) COMPETITION INSTRUMENTED -- EFFECTS ON EMPLOYMENT/////////////
////////////////////////////////////////////////////////////////////////////////		

* Create Table C12	

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1
	keep if robotsfirm == 0
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	// Create relevant variables
	capture g logincome_adj = ln(income_adj)
	capture g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture egen beidXyear = group(beid year)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)
	egen industry_ifrXyear = group(industry_ifr year)

	g competeXrepl = compete*replaceable
	g competeXnonrepl = compete*(1-replaceable)
	g competeXmeffect = compete*meffect
	g competeXnonmeffect = compete*(1-meffect)
	g competeXlowskilled = compete*lowskilled
	g competeXnonlowskilled = compete*(1-lowskilled)
	g competeXmedskilled = compete*medskilled
	g competeXnonmedskilled = compete*(1-medskilled)
	g competeXhighskilled = compete*(1-lowmedskilled)

	g robotsexpXreplaceable = robotsexposure_tmin5*replaceable
	g robotsexpXnonreplaceable = robotsexposure_tmin5*(1-replaceable)
	g robotsexpXmeffect = robotsexposure_tmin5*meffect
	g robotsexpXnonmeffect = robotsexposure_tmin5*(1-meffect)
	g robotsexpXlowskilled = robotsexposure_tmin5*lowskilled
	g robotsexpXnonlowskilled = robotsexposure_tmin5*(1-lowskilled)
	g robotsexpXmedskilled = robotsexposure_tmin5*medskilled
	g robotsexpXnonmedskilled = robotsexposure_tmin5*(1-medskilled)
	g robotsexpXhighskilled = robotsexposure_tmin5*(1-lowmedskilled)
	
	
ivreghdfe empl (compete=robotsexposure_tmin5) [weight=indweight], absorb(beid  $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl23
ivreghdfe empl (compete=robotsexposure_tmin5) $xvar $xvaradd [weight=indweight], absorb(beid  $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl24
ivreghdfe empl (compete=robotsexposure_tmin5) $xvar [weight=indweight], absorb(beid  $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl25	
ivreghdfe loghoursworked (compete=robotsexposure_tmin5) [weight=indweight], absorb(beid  $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl26
ivreghdfe loghoursworked (compete=robotsexposure_tmin5) $xvar $xvaradd [weight=indweight], absorb(beid  $fevarcompete) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl27
ivreghdfe loghoursworked (compete=robotsexposure_tmin5) $xvar [weight=indweight], absorb(beid  $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1 & robotsfirm==0
	estimates store repl28		

qui estimates restore repl23
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster replace word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl24
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl25
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl26
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl27
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl28
    outreg2 using "Robots and Workers\Results\Worker_competeIV_employment", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
 	

////////////////////////////////////////////////////////////////////////////////
///////5) COMPETITION INSTRUMENTED BY WORKER TYPE -- EFFECTS ON EMPLOYMENT//////
////////////////////////////////////////////////////////////////////////////////		
	
* Create Table C13

use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1
	keep if robotsfirm == 0
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	// Create relevant variables
	capture g logincome_adj = ln(income_adj)
	capture g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture egen beidXyear = group(beid year)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)
	egen industry_ifrXyear = group(industry_ifr year)

	g competeXrepl = compete*replaceable
	g competeXnonrepl = compete*(1-replaceable)
	g competeXmeffect = compete*meffect
	g competeXnonmeffect = compete*(1-meffect)
	g competeXlowskilled = compete*lowskilled
	g competeXnonlowskilled = compete*(1-lowskilled)
	g competeXmedskilled = compete*medskilled
	g competeXnonmedskilled = compete*(1-medskilled)
	g competeXhighskilled = compete*(1-lowmedskilled)

	g robotsexpXreplaceable = robotsexposure_tmin5*replaceable
	g robotsexpXnonreplaceable = robotsexposure_tmin5*(1-replaceable)
	g robotsexpXmeffect = robotsexposure_tmin5*meffect
	g robotsexpXnonmeffect = robotsexposure_tmin5*(1-meffect)
	g robotsexpXlowskilled = robotsexposure_tmin5*lowskilled
	g robotsexpXnonlowskilled = robotsexposure_tmin5*(1-lowskilled)
	g robotsexpXmedskilled = robotsexposure_tmin5*medskilled
	g robotsexpXnonmedskilled = robotsexposure_tmin5*(1-medskilled)
	g robotsexpXhighskilled = robotsexposure_tmin5*(1-lowmedskilled)
			
ivreghdfe loghoursworked (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl61
ivreghdfe loghoursworked (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl62
ivreghdfe loghoursworked                   (competeXmeffect=robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl63	
ivreghdfe loghoursworked (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl64
ivreghdfe loghoursworked (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl65
ivreghdfe loghoursworked                (competeXrepl=robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl66
ivreghdfe loghoursworked (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl67
ivreghdfe loghoursworked (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl68
ivreghdfe loghoursworked                   (competeXlowskilled=robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl69	
	
qui estimates restore repl61
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster replace word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl62
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl63
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl64
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl65
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl66
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl67
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl68
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl69
	outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_hourswint", label aster append word tex ctitle(Hours worked \textit{(log)}) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))		

		
ivreghdfe empl (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl81
ivreghdfe empl (competeXnonmeffect competeXmeffect=robotsexpXnonmeffect robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl82
ivreghdfe empl                   (competeXmeffect=robotsexpXmeffect) meffect $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl83	
ivreghdfe empl (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl84
ivreghdfe empl (competeXnonrepl competeXrepl=robotsexpXnonreplaceable robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl85
ivreghdfe empl                (competeXrepl=robotsexpXreplaceable) replaceable $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl86
ivreghdfe empl (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevarcompete) cluster($clustercompete), if manufacturing==1
	estimates store repl87
ivreghdfe empl (competeXnonlowskilled competeXlowskilled=robotsexpXnonlowskilled robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl88
ivreghdfe empl                   (competeXlowskilled=robotsexpXlowskilled) lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevarcompete rinpersoon) cluster($clustercompete), if manufacturing==1
	estimates store repl89	
	
qui estimates restore repl81
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster replace word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat)) addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl82
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl83
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl84
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl85
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl86
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl87
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl88
qui outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))
qui estimates restore repl89
	outreg2 using "Robots and Workers\Results\Worker_competeIV_hetero_emplext", label aster append word tex ctitle(Employed) nocon nor2 addstat("Kleibergen-Paap \textit{F}-statistic",e(widstat))				
	

	
	
////////////////////////////////////////////////////////////////////////////////
///6) COMPETITION INSTRUMENTED BY WORKER TYPE -- EFFECTS ON PERSONAL INCOME/////
////////////////////////////////////////////////////////////////////////////////		

* Create Table C14	
	
use "Robots and Workers\Data\Workers_PS_LFS_panel_full.dta", clear
	merge m:1 isco08 using "Robots and Workers\Base Files\Replaceable.dta", keep(3 4 5) keepusing(bluecollar replaceable replaceable_orig meffect) update replace nogen
	keep if hwweight == 1
	sort rinpersoon year

	g income_adj = income_gross
	g fyear = year if nonempl == 0
	g unemplyears = 0
	replace unemplyears = nonempl+unemplyears[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
	foreach var in beid sbi2 sbi3 sbi4 mun compete robots robotsfirm manufacturing logvalueaddedpworker09 logworkers09 income_gross fyear {
		if "`var'" != "income_gross" {
			replace `var' = `var'[_n-1] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
		if "`var'" == "income_gross" {
			replace income_adj = 0.7*income_adj[_n-unemplyears] if nonempl==1 & rinpersoon[_n-1]==rinpersoon
		}
	}
	keep if lfsyeardiff <= 9 & include==1 & include_post2009==1
	keep if robotsfirm == 0
	merge m:1 beid fyear using "Robots and Workers\Data\BEID_shiftshare.dta", nogen keep(1 3)

	// Create relevant variables
	capture g logincome_adj = ln(income_adj)
	capture g empl = 1-nonempl
	replace empl = . if (jobtype_selfempl==1 | jobtype_retired==1 | jobtype_student==1 | jobtype_other==1 | jobtype_employee==.)
	replace empl = . if rinpersoon[_n-1]==rinpersoon[_n] & empl[_n-1]==0
	capture egen beidXyear = group(beid year)
	capture g sbi2 = substr(sbi,1,2)
	capture destring sbi2, force replace
	capture g medskilled = (1-lowskilled)*(lowmedskilled==1)
	egen industry_ifrXyear = group(industry_ifr year)

	g competeXrepl = compete*replaceable
	g competeXnonrepl = compete*(1-replaceable)
	g competeXmeffect = compete*meffect
	g competeXnonmeffect = compete*(1-meffect)
	g competeXlowskilled = compete*lowskilled
	g competeXnonlowskilled = compete*(1-lowskilled)
	g competeXmedskilled = compete*medskilled
	g competeXnonmedskilled = compete*(1-medskilled)
	g competeXhighskilled = compete*(1-lowmedskilled)

	g robotsexpXreplaceable = robotsexposure_tmin5*replaceable
	g robotsexpXnonreplaceable = robotsexposure_tmin5*(1-replaceable)
	g robotsexpXmeffect = robotsexposure_tmin5*meffect
	g robotsexpXnonmeffect = robotsexposure_tmin5*(1-meffect)
	g robotsexpXlowskilled = robotsexposure_tmin5*lowskilled
	g robotsexpXnonlowskilled = robotsexposure_tmin5*(1-lowskilled)
	g robotsexpXmedskilled = robotsexposure_tmin5*medskilled
	g robotsexpXnonmedskilled = robotsexposure_tmin5*(1-medskilled)
	g robotsexpXhighskilled = robotsexposure_tmin5*(1-lowmedskilled)
		
	
reghdfe logearnings competeXnonmeffect competeXmeffect meffect $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl41
reghdfe logearnings competeXnonmeffect competeXmeffect meffect $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl42
reghdfe logearnings                   competeXmeffect meffect $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl43	
reghdfe logearnings competeXnonrepl competeXrepl replaceable $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl44
reghdfe logearnings competeXnonrepl competeXrepl replaceable $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl45
reghdfe logearnings                competeXrepl replaceable $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl46
reghdfe logearnings competeXnonlowskilled competeXlowskilled lowskilled $xvar $xvaradd [weight=indweight], absorb(beid $fevar) cluster($cluster), if manufacturing==1
	estimates store repl47
reghdfe logearnings competeXnonlowskilled competeXlowskilled lowskilled $xvar [weight=indweight], absorb(beid $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl48
reghdfe logearnings                competeXlowskilled lowskilled $xvar [weight=indweight], absorb(beid#fyear $fevar rinpersoon) cluster($cluster), if manufacturing==1
	estimates store repl49


qui estimates restore repl41
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster replace word tex ctitle(Earnings \textit{(log)}) nocon addnote(Notes: We include year fixed effects in all specifications, firm fixed effects in columns (2)-(5), industry 4-digit-by-year fixed effects in columns (2)-(5), municipality-by-year fixed effects in columns (3)-(5), worker fixed effects in columns (4)-(5) and firm-by-year fixed effects in column (5).),
qui estimates restore repl42
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl43
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl44
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl45
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl46
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl47
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl48
qui outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon
qui estimates restore repl49
	outreg2 using "Robots and Workers\Results\Worker_compete_hetero_earnings", label aster append word tex ctitle(Earnings \textit{(log)}) nocon				
	
		